package com.mida.app.report.controller;

import com.mida.app.report.service.DocStructTemplateAuthSpaceService;
import com.mida.app.report.service.MdmService;
import com.mida.bms.report.api.DocStructTemplateAuthSpaceApi;
import com.mida.bms.report.entity.DocStructTemplateAuthSpace;
import com.mida.bms.report.vo.DocStructTemplateAuthSpaceVo;
import com.mida.bms.report.vo.PageResultVo;
import com.mida.dms.mdm.dto.UserDto;
import com.mida.dms.mdm.vo.provider.BaseOrgInfoVo;
import com.mida.dms.mdm.vo.provider.BusinessUnitVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.cons.RespConst;
import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：wang xiao le
 * @description：结构化模板授权空间
 * @date ：2021/3/30 10:10
 */
@RestController
@RequestMapping("/docStructTemplateAuth")
@CrossOrigin
@Slf4j
public class DocStructTemplateAuthSpaceController {

    /**
    * 结构化模板授权空间Service
    * */
    @Autowired
    private DocStructTemplateAuthSpaceService docStructTemplateAuthSpaceService;

    @Autowired
    private DocStructTemplateAuthSpaceApi docStructTemplateAuthSpaceApi;


    @Autowired
    private MdmService mdmService;
    /**
     * 1、添加结构化模板报告授权空间
     *
     * @param docStructTemplateAuthSpaceVo   结构化模板报告授权空间Vo
     * @return
     */
    @PostMapping(value = "add",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ActionResult add(@RequestBody DocStructTemplateAuthSpaceVo docStructTemplateAuthSpaceVo) throws Exception {
        return  docStructTemplateAuthSpaceApi.add(docStructTemplateAuthSpaceVo);
    }


    /**
     * 2、更新结构化模板报告授权空间
     *
     * @param docStructTemplateAuthSpaceVo
     * @return
     */
    @PostMapping(value = "update",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ActionResult update( @RequestBody  DocStructTemplateAuthSpaceVo docStructTemplateAuthSpaceVo) throws Exception {
        return  docStructTemplateAuthSpaceApi.update(docStructTemplateAuthSpaceVo);
    }

    /**
     * 3、删除结构化模板报告授权空间
     *
     * @param stTempAuthSpSoid 结构化模板报告授权空间soid
     * @return null
     */
    @DeleteMapping(value = "/delete")
    public ActionResult delete(@RequestParam(value = "stTempAuthSpSoid") long stTempAuthSpSoid) {

        return  docStructTemplateAuthSpaceApi.delete(stTempAuthSpSoid);
    }

    /**
     * 4、获取单个结构化模板报告授权空间
     *
     * @param stTempAuthSpSoid 结构化模板报告授权空间soid
     * @return
     */
    @GetMapping("get")
    public ListResult<DocStructTemplateAuthSpaceVo> get(@RequestParam(value = "stTempAuthSpSoid") long stTempAuthSpSoid) {
        return  docStructTemplateAuthSpaceService.get(stTempAuthSpSoid);
    }

    /**
     * 5、分页查询结构化模板报告授权空间
     * @param searChData  搜索关键字
     * @param currentPage 第几页
     * @param pageSize    每页显示数
     * @return
     */
    @ApiOperation(value = "分页查询结构化模板报告授权空间", notes = "分页查询结构化模板报告授权空间")
    @PostMapping(value = "/findPage",produces = { "application/json;charset=UTF-8" })
    public ListResult<PageResultVo<DocStructTemplateAuthSpaceVo>> findPage(@RequestParam(value = "searchData") String searChData,
                                                                           @RequestParam(value = "currentPage") Long currentPage,
                                                                           @RequestParam(value = "pageSize") Long pageSize) {

        return  docStructTemplateAuthSpaceApi.findPage(searChData, currentPage, pageSize);

    }
    /**
     * 6、结构化模板报告授权空间名称重复性校验
     */
    @GetMapping(value = "/checkName")
    public ActionResult checkName(@RequestParam(value = "name", required = true) String name,
                                  @RequestParam(value = "stTempAuthSpSoid", required = false) Long stTempAuthSpSoid) {

        return  docStructTemplateAuthSpaceApi.checkName(name,stTempAuthSpSoid);

    }

    /**
     * 7、更改结构化模板报告授权空间状态
     *
     * @param stTempAuthSpSoid 授权空间soid
     * @param status       状态soid
     * @return
     */
    @GetMapping("/updateStatus")
    public ActionResult updateStatus(@RequestParam(value = "status", required = true) Long status,
                                     @RequestParam(value = "stTempAuthSpSoid", required = false) Long stTempAuthSpSoid) {

        return  docStructTemplateAuthSpaceApi.updateStatus(status, stTempAuthSpSoid);
    }

    /**
     * 8、结构化模板报告授权空间下拉(查询所有授权空间)
     * @return
     */
    @GetMapping("/findAll")
    public ListResult<List<DocStructTemplateAuthSpace>> findAll(@RequestParam(value = "searchData") String searChData){
        try {
            return  docStructTemplateAuthSpaceService.findAll(searChData);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("查询所有授权空间失败,请联系管理员"));
        }
    }


    /**
     * 1、根据当前用户员工soid查询医院
     *
     * @return
     */
    @GetMapping(value = "/findHospitalByEmployeeSoid")
    public ListResult<List<BaseOrgInfoVo>> findHospitalByEmployeeSoid(HttpServletRequest request){
        ListResult<UserDto> userDtoresult = mdmService.getLoginUserInfo();
        UserDto loginUser = userDtoresult.getData();
        Long employeeSoid1 = loginUser.getEmployeeSoid();
        return mdmService.findHospitalByEmployeeSoid(employeeSoid1);
    }

    /**
     * 2、查询医院下的所有业务单元
     * @param hospitalSoid 医院soid
     * @param buTypeCode 业务单元类型
     * @param search 检索条件:名称，代码，拼音码，五笔码
     */
    @GetMapping(value = "/findBusinessUnitListByHospitalSoid")
   ListResult<List<BusinessUnitVo>> findBusinessUnitListByHospitalSoid(@RequestParam("hospitalSoid")Long hospitalSoid,
                                                                       @RequestParam("buTypeCode")Long buTypeCode,
                                                                       @RequestParam(value = "search",defaultValue = "") String search){
        return mdmService.findBusinessUnitListUnderHospital(hospitalSoid,buTypeCode,search);
    }
    /**
     *3.1根据医院soid模糊查询当前医院下员工信息
     * @param search 检索条件:工号,名称,拼音码，五笔码，
     * @param orgInfoSoid 组织soid （医院soid）
     * @param orgInfoType 组织类型:集团-2585、医院-2586、科室-2587
     * @return
     */
    @GetMapping("/findEmployeeListByOrgSoid")
    public ListResult<List<EmployeeInfoVo>> findEmployeeListByOrgSoid(@RequestParam("orgInfoSoid")Long orgInfoSoid,
                                                                      @RequestParam("orgInfoType")Long orgInfoType,
                                                                      @RequestParam(value = "search",defaultValue = "")String search){
        return mdmService.findListByOrgSoid(orgInfoSoid, 2586L, search);
    }

    /**
     *3.2 根据业务单元soid模糊查询员工信息
     * @param search 检索条件:工号,名称,拼音码，五笔码，
     * @param businessUnitSoidStr 业务单元soid集合
     * @return
     */
    @GetMapping(value = "/findEmployeeListByBusinessUnitSoid",produces = MediaType.APPLICATION_JSON_VALUE)
    public ListResult<List<EmployeeInfoVo>> findEmployeeListByBusinessUnitSoid(@RequestParam("businessUnitSoidStr")String  businessUnitSoidStr,
                                                                @RequestParam(value = "search",defaultValue = "")String search){
        return mdmService.findListByBusinessUnitSoidList(businessUnitSoidStr,search);
    }
}
